improved the code a bit