Moving code into template, code cleanups.