add function and clean the code a bit