Fixed bugs and added corner cases