parsing algorithm rewritten