Fixed bugs, removed some magic numbers.