this is updated prog2. needs testing.