Cleaned up code significantly

Still needs to be gone over in more detail