Some bug-fixes, and remove redundant code