fixed a bug with edge cases