debugged, should work properly now