Added tool functions

to remove code duplication and improve readability of code.