Beginning some sort of complete rewrite. code is really messy atm