Fixed all potential bugs pointed out by Dr. Brinkman