Basic working code, improved stability.