Re factored the code