some code improvments based on trapnine's comments