refatored the code a little