implemented dynamic template loading and some cleanup