Lots of bug fixing