Cleaned up code, is now a lot clearer, added some functions for easier manipulation later on.