Bug fixes

A bunch of bug fixes