fixed critical bugs

merged properly so that it works