Added function implementation and comments