Various bug fixes

A couple of bugs have been fixed