Refactor code to avoid duplication for priority and fee calculations

Other improvments including storing more information to file