Removed dead code and moved code for readability