Refactored slightly, added Yen's optimization