Organized code a bit and replaced some magic numbers with defined constants