Added some logic
Fixed some minor bugs