improve the calc using priority stack and fix some bugs