Final working version before optimizing