Added utility functions and further logic