fixed more subtle bugs to fix the main bug reported by Nathan