cleaned up the code significantly