Refactored custom functions to include waits