code refactoring, fixes to gallery