cleanup code, add functions to make it a bit more clear