refactored some of the code, still not working though it should