moved some code, removed some redundant variables