Small fixes - now reconciling quite closely with Javascript code