refctor code a bit