improved code a bit