Overhaul of function implementation