Cleaned up code and made it a bit less hacky