Fixed loads of bugs

Issues #1, #2, #3 fixed