implement most logic, not reviewed... still not working