Cleaned up code quite a bi to make it more readable and understandable.