Added more error handling (looks nice and clean). Also added template caching. And more comments.