copied functions from main to global scope, implemented HAC