Code now works. More debugging is needed.