code refactor and updates to functions.