Simplified some code, added a few functions, formatted nicer