Changed everything to using templates, fixed a few bugs here and there