cleaned up... need to test this first though
cleaned up references; made more modular