fix small logic error
various improvements