Improve output format and cleanup code