Cleaned up code significantly