Corrected some bugs, thanks to Alex