made this code a little bit more readable, it's still a complete mess though