Fixed various bugs