some bug fixes and cleanup using the with function