Fixed several bugs from previous commit