Refatored the code a bit