added bgimage and refactored some repeated code.