dramatically cleaned up code