refactoring and threading parsing algorithm