refactoring and some final cleanup
template v1 ready now