Fixed around 5 bugs found in code