Fixing some bugs and adding some extra functionality