Miscellanious bug fixes and cleanup