fixed controller issue and made code more flexible