cleaned up code based on feedback and added optional parameters