fix for edge cases and removed some unnecessary code