fixed a lot of edge cases