Cleanup and provide mostly working version of the function