Separated functions from main program and improved data extraction algorithm