Rewrote some code, added templates/functions: