This is a ported version of the best complete application from 12/7/13